#!/bin/bash
### Usage: This script use to config linux system
### Author:Rex yang
### Modify:Rex yang
### Email: yangshangjun@corp.the9.com
### First variable setting

outip=`ifconfig eth0|grep inet|cut -f 2 -d ":" |cut -f 1 -d " "|awk -F "." '{print $4}'`
outip1=`ifconfig eth0|grep inet|cut -f 2 -d ":" |cut -f 1 -d " "|awk -F "." '{print $1}'`
outip2=`ifconfig eth0|grep inet|cut -f 2 -d ":" |cut -f 1 -d " "|awk -F "." '{print $2}'`
outip3=`ifconfig eth0|grep inet|cut -f 2 -d ":" |cut -f 1 -d " "|awk -F "." '{print $3}'`
outip4=$outip3
outip4=$((outip4+1))

gw1=`ifconfig eth0|grep inet|cut -f 3 -d ":" |cut -f 1 -d " "|awk -F "." '{print $3}'`
gw2=$gw1
gw2=$((gw2-7))
ntpip=10.10.255.1


echo -n "Enter new name for hostname and press [ENTER]: "
read name

hostname=$name
password=myjsy89wqt

yum install -y sysstat

### service config
for i in `chkconfig --list | awk '{print $1}' `; do echo $i; chkconfig $i off; done
for i in sshd rsyslog network crond sysstat acpid lm_sensors irqbalance iptables; do chkconfig $i on; done

### sshd config
echo "Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 600
PermitRootLogin yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
IgnoreRhosts yes
HostbasedAuthentication no
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UseDNS no
MaxStartups 20
Subsystem       sftp    /usr/libexec/openssh/sftp-server" > /etc/ssh/sshd_config
mkdir ~/.ssh/
chmod 700 ~/.ssh/
echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAuEnNsaaXRn1tsG9AuelQZi/3affSjGHF7I1dROiZ9+MVXxXI4nuH/7VvoQkZvLT8VKUBW3jb35ld+NXYCWUlwCR20wk05lxIqKBRv0xbrlSnbK+kW1e7kUTNGk9kJGXPsys39A0w8vbc8ELHN8KJ342ll84gJ903TmQzCURqnuNRToUgPccTTNWFY27zeqSVupBv3/Ka7NwOGiCG11knVLt9nZ9UrlOD26+zAcOPLu870/n12feM8m7A0M7eSEi98xBhQEMMLkA9RvGI9Qt0SOCKsGSTBix+/yv6xM6VsvdFQJDohmYZFEoo1DX1aKnVZLYo/QHkV76Ww8E3F80KdQ== yangshangjun
#key" > /root/.ssh/authorized_keys
service sshd restart
#echo 'bash /opt/zabbix/zabbix' >> /etc/rc.local
#echo 'NETWORKING=yes' > /etc/sysconfig/network
#echo "HOSTNAME=$hostname" >> /etc/sysconfig/network

###bashrc
echo 'umask 022' >> /etc/bashrc
echo 'ulimit -c unlimited' >> /etc/bashrc
echo 'ulimit -n 65535' >> /etc/bashrc

###crontab
crontab -l >> /tmp/crontab2.tmp
echo '15 1 * * * /usr/sbin/ntpdate "$ntpip";/usr/sbin/hwclock -w > /dev/null 2>&1' >> /tmp/crontab2.tmp
echo '*/5 * * * * /bin/bash /opt/zabbix/system_check.sh' >> /tmp/crontab2.tmp
crontab /tmp/crontab2.tmp
rm -fr /tmp/crontab2.tmp

###sysctl
cat >> /etc/sysctl.conf << END
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296

net.core.wmem_max = 873200
net.core.rmem_max = 873200
net.core.somaxconn = 256
net.core.netdev_max_backlog = 1000

net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_mem = 786432  1048576 1572864
net.ipv4.tcp_wmem = 8192  436600  873200
net.ipv4.tcp_rmem = 32768  436600  873200
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
END

#edit openfile
cat >> /etc/security/limits.conf <<EOF
*	soft	nofile	65535
*	hard	nofile	65535
*	soft	nproc	65535
*	hard	nproc	65535
EOF
sed -i 's#1024#65535#' /etc/security/limits.d/90-nproc.conf

## edit inittab file ##
#sed -i '/tty6/a co:2345:respawn:/sbin/agetty -h -L 19200 ttyS0 vt100' /etc/inittab

## edit securetty file ##
#sed -i '/tty11/a ttyS0' /etc/securetty

## edit grub.conf bmc
temp1=`cat /etc/grub.conf |head -7 |tail -1 |awk -F "root=" {'print $2'}`
temp2=`/sbin/e2label $temp1`
#sed -i '/LABEL=\'$temp2'/s//LABEL=\'$temp2' console=tty0 console=ttyS0,19200/g' /boot/grub/grub.conf

## edit /etc/init.d/network add route ##
#sed -i '/exit \$rc/i /sbin/route add -net 10.0.0.0/8 gw '$outip1'.'$outip2'.'$gw2'.254' /etc/init.d/network
#sed -i '/exit \$rc/i /sbin/route add -net 192.168.0.0/16 gw '$outip1'.'$outip2'.'$gw2'.254' /etc/init.d/network

## edit bmc address ##
#ipmitool -I open lan set 1 ipaddr $outip1.$outip2.$outip4.$outip
#ipmitool -I open lan set 1 defgw ipaddr $outip1.$outip2.$gw2.254
#ipmitool -I open lan set 1 netmask 255.255.248.0

## add zabbix user ##
/usr/sbin/useradd zabbix -s /sbin/nologin

#echo $password | passwd root --stdin

###reboot
#sleep 10
#reboot
